using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using Spine.Unity;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public class GenArtPrefabSpine : GenArtPrefabBase
    {
        public override ArtModeGenType genPrefabType => ArtModeGenType.Spine;
        protected override string outputBasePath => "Res/03_Prefabs/Spine";
        protected override int prefabLayer => (int)LayerConsts.Default;

        public override void GenArtPrefab(bool withAnimator, bool forceReplaceOld)
        {
            var rAsset = SpineManager.LoadAsset(this.entityName);
            if (rAsset != null)
            {
                var rSkeleton = SkeletonAnimation.NewSkeletonAnimationGameObject(rAsset);
                var rGo = PrefabUtility.SaveAsPrefabAssetAndConnect(rSkeleton.gameObject, "Assets/" + PathConstant.GetSpinePrefabPath(this.entityName), InteractionMode.AutomatedAction, out var success);
                if (success)
                {
                    EditorUtility.SetDirty(rGo);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                }
                else
                {
                    Debug.LogError($"保存Prefab失败:[{this.entityName}]");
                }
                UnityEngine.Object.DestroyImmediate(rSkeleton.gameObject);
            }
            
        }

        public override string GetArtModeResPath() => string.Empty;
    }
}